//
//  MyselfViewController.m
//  miaopai
//
//  Created by Crow Zhang on 15/11/12.
//  Copyright © 2015年 Crow Zhang. All rights reserved.
//

#import "MyselfViewController.h"
#import "CollectionViewController.h"

#import "HeartVideoViewController.h"

#import "MyVideoViewController.h"
#import <SVProgressHUD.h>
#import <MessageUI/MFMailComposeViewController.h>

@interface MyselfViewController ()<UITableViewDataSource, UITableViewDelegate, MFMailComposeViewControllerDelegate>

@property(nonatomic, assign)BOOL isPush;

@property(nonatomic, retain)UITableView *tableView;


@end

@implementation MyselfViewController

- (void)dealloc
{
    [_tableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];


    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] init];
    backItem.title = @"返回";
    self.navigationItem.backBarButtonItem = backItem;
    [backItem release];
    
    self.title = @"我的";
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"myselfTableViewCell"];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.tableView];
    [_tableView release];

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (void)buttonAction:(UIButton *)button{
    CollectionViewController *collection = [[CollectionViewController alloc] init];
    [self.navigationController pushViewController:collection animated:YES];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 3;
    }else
        return 3;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"myselfTableViewCell" forIndexPath:indexPath];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"我的收藏";
        }else if(indexPath.row == 1)
        {
            cell.textLabel.text = @"红心视频";
        }else if(indexPath.row == 2)
        {
            cell.textLabel.text = @"我的视频";
        }
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            cell.textLabel.text = @"清理缓存";
        }else if (indexPath.row == 1)
        {
            cell.textLabel.text = @"关于我们";
        }else if (indexPath.row == 2)
        {
            cell.textLabel.text = @"问题反馈";
        }
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            CollectionViewController *collection = [[CollectionViewController alloc] init];
            [self.navigationController pushViewController:collection animated:YES];
            [collection release];
            self.isPush = YES;
        }else if(indexPath.row == 1)
        {
            HeartVideoViewController *heartVideoViewController = [[HeartVideoViewController alloc] init];
            [self.navigationController pushViewController:heartVideoViewController animated:YES];
            [heartVideoViewController release];
            self.isPush = YES;
        }else if(indexPath.row == 2)
        {
            MyVideoViewController *video = [[MyVideoViewController alloc] init];
            [self.navigationController pushViewController:video animated:YES];
            [video release];
            self.isPush = YES;
        }
    }
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            [SVProgressHUD showSuccessWithStatus:@"清理成功!"];
        }else if(indexPath.row == 1)
        {
            
        }else
        {
            if ([MFMailComposeViewController canSendMail]) { // 用户已设置邮件账户
                // 调用发送邮件的代码
                
                MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
                [mc setMailComposeDelegate:self];
                //    mc.mailComposeDelegate = self;
                
                // 设置邮件主题
                [mc setSubject:@"问题反馈"];
                
                [mc setToRecipients:@[@"crowzhang@foxmail.com"]];
                //[mc setCcRecipients:@[@"18840861676@163.com"]];
                //    [mc setBccRecipients:[NSArray arrayWithObject:@"secret@gmail.com"]];
                [mc setMessageBody:@"请留下您的问题，我们将在下一个版本中解决，谢谢您！" isHTML:NO];
                // 弹出邮件发送视图
                [self presentViewController:mc animated:YES completion:nil];
                
            }
        }
    }
}

- (void)mailComposeController:(MFMailComposeViewController*)controller
          didFinishWithResult:(MFMailComposeResult)result
                        error:(NSError*)error {
    switch (result)
    {
        case MFMailComposeResultCancelled:
            NSLog(@"Mail send canceled...");
            break;
        case MFMailComposeResultSaved:
            NSLog(@"Mail saved...");
            break;
        case MFMailComposeResultSent:
            NSLog(@"Mail sent...");
            [SVProgressHUD showSuccessWithStatus:@"发送成功!"];
            break;
        case MFMailComposeResultFailed:
            NSLog(@"Mail send errored: %@...", [error localizedDescription]);
            [SVProgressHUD showErrorWithStatus:@"发送失败!"];
            break;
        default:
            break;
    }
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewWillAppear:(BOOL)animated
{
    //self.navigationController.interactivePopGestureRecognizer.delegate = (id) self;
    if (!self.isPush) {
        UIView * view = [self.tabBarController.view.subviews lastObject];
        [UIView animateWithDuration:0.5 animations:^{
            view.frame = CGRectMake(0, kHeight - 48, kWidth, 48);
        }];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    if (self.isPush) {
        self.isPush = NO;
    }
    
}



@end
